/**
 * 栏目页面的js
 */
var pageNo = 1; //当前页码
var limit = 14; //每页的新闻数量
var columnId = -1;
 //查询
 function search(columnid){
	columnId = columnid;
 	var paramData = {};
 	paramData["pageNo"] = pageNo;
 	paramData["limit"] = limit;
	paramData["columnid"] = columnid;
 	//获取查询数据
 	$.post(realPath+"/path/newsinfo/getNewsByColumn", paramData,
 			function(result) {
 				if (result.state == 'Success') {
 					//显示新闻列表
 					showNewsList(result.content);
 				} else {
 					alert("操作失败,原因："+result.content);
 				}
 			}
 		);
 }

 //显示新闻列表
 function showNewsList(data){
 	//显示新闻详情
 	var content = "<ul class=\"mod-resultList-ul clearfix\">";
 	for(var i=0; i<data.items.length; i++){
 	 	var newsUrl = realPath + "/path/newsinfo/frontNewsInfo?newsId=" + data.items[i]["id"];
 		content += "<li><div class=\"mod-resultList-abstract clearfix\"><div class=\"img\">";
 		content += "<a href=\"" + newsUrl + "\" target=\"_blank\">";
 		content += "<img src=\"" + realPath + data.items[i]["coverPath"] + "\" width=\"\" height=\"\">";
 		content += "</a></div><div class=\"mod-resultList-hd\"><h3 class=\"mod-resultList-title\">";
 		content += "<a href=\"" + newsUrl + "\" target=\"_blank\">" + data.items[i]["title"] + "</a></h3></div>";
 		content += "<div class=\"mod-resultList-bd\">";
 		var nabstract = data.items[i]["nabstract"];
 		if(nabstract == null) nabstract = ""; //判断摘要是否为null
 		content += "<p class=\"mod-resultList-info mb5\">" + nabstract + "</p>";
 		content += "<div class=\"mod-resultList-viewTime clearfix\"><span class=\"fn-fl\"><i class=\"glyphicon glyphicon-eye-open mr5\"></i>"+data.items[i]["pageViewCounter"]+"</span>";                        
 		content += "<span class=\"fn-fr\"><i class=\"glyphicon glyphicon-time mr5\"></i>" + data.items[i]["publishTime"] + "</span>";
 		content += "</div></div></div> </li>";
 	}
 	
 	content += "</ul>";
 	
 	document.getElementById ("newsList").innerHTML = content;
 	//分页
 	pagnation(data.totalCount,data.totalPageCount,data.currentPageNo);
 }
 
 //改变内容中关键字的样式
 function changeKeyWordStytle(content, keyword){
 	if(content == null) return "";
 	if(keyword == "") return content;
 	//判断是否包含字符串
 	var newContent = content;
 	if(content.indexOf(keyword) > -1){
 		//拆分字符串
 		var str=content.split(keyword);
 		newContent = str[0];
 		for (var i=1;i<str.length ;i++ )   
 	    { 
 			newContent += "<em>" + keyword + "</em>"; //关键字颜色变化
 			newContent += str[i];
 	    }
 	}
 	return newContent;
 }
 
//显示页面
function pagnation(totalCounts,totalPages,currentPageNo){
	var pagnation = $("#pagnation");
	var pagnationHtml = "";
	var pagePrevHtml = "";
	var pageNextHtml = "";
	var pageNumberHtml = "";
	
	//暂时先不用加载条数记录
	//pagnationHtml += "<span class=\"pageDown-sum\"><em>共<i>"+totalCounts+"</i>条记录</em><em>当前第 <i>"+currentPageNo+"</i>页 </em><i>/</i>共<i> "+totalPages+"</i>页</span>";
	if(totalPages > 0)
	{
		if(currentPageNo == 1)
		{
			pagePrevHtml = "<a class=\"page-prev\" title=\"上一页\">上一页</a>";
			//总页码为 1，禁止下一页
			if(totalPages==1)
			{
				pageNextHtml = "<a class=\"page-next\" title=\"下一页\">下一页</a>";
			}else{
				pageNextHtml = "<a href=\"javascript:goPage("+parseInt(currentPageNo+1)+")\" class=\"page-next\" title=\"下一页\">下一页</a>";	
			}
		}else if(currentPageNo < totalPages){
			pagePrevHtml = "<a href=\"javascript:goPage("+parseInt(currentPageNo-1)+")\" class=\"page-prev\" title=\"上一页\">上一页</a>";
			pageNextHtml = "<a href=\"javascript:goPage("+parseInt(currentPageNo+1)+")\" class=\"page-next\" title=\"下一页\">下一页</a>";
		}else if(currentPageNo == totalPages){
			pagePrevHtml = "<a href=\"javascript:goPage("+parseInt(currentPageNo-1)+")\" class=\"page-prev\" title=\"上一页\">上一页</a>";
			pageNextHtml = "<a class=\"page-next\">下一页</a>";
		}
		//总页码 <7
		if(totalPages < 7){
			for(var i = 0 ; i < totalPages ; i++){
				var currentClass = "";
				if((i+1) == currentPageNo){
					currentClass = "class = \"current\"";
				}
				pageNumberHtml += "<a href=\"javascript:goPage("+parseInt(i+1)+")\" "+currentClass+">"+(i+1)+"</a>";
			}
		}else{
			//判断是否后几页
			if(totalPages - currentPageNo - 4 > 0){
				var start = 0,end = 0;
				if(currentPageNo  < 3){
					start = 0;end = 6;
				}else{
					start = currentPageNo - 3 ; end = currentPageNo + 3;
				}
				for(var i = start ; i < end ; i++){
					var currentClass = "";
					if((i+1) == currentPageNo){
						currentClass = "class = \"current\"";
					}
					pageNumberHtml += "<a href=\"javascript:goPage("+parseInt(i+1)+")\" "+currentClass+">"+(i+1)+"</a>";
				}
				pageNumberHtml += "<span class=\"page-items\">...</span>";
				pageNumberHtml += "<a href=\"javascript:goPage("+totalPages+")\" >"+totalPages+"</a>";
			}else{
				pageNumberHtml += "<a href=\"javascript:goPage(1)\" >1</a>";
				pageNumberHtml += "<span class=\"page-items\">...</span>";
				for(var i =totalPages - 6 ; i < totalPages ; i++){
					var currentClass = "";
					if((i+1) == currentPageNo){
						currentClass = "class = \"current\"";
					}
					pageNumberHtml += "<a href=\"javascript:goPage("+parseInt(i+1)+")\" "+currentClass+">"+(i+1)+"</a>";
				}
			}
		}
		
		pagnation.html(pagnationHtml + pagePrevHtml + pageNumberHtml + pageNextHtml);
	}else{
		pagnation.html("<font color='red' size='+2'>没有要显示的数据</font>");
	}
	
}

function goPage(number){
	pageNo = number;
	search(columnId);
}

$(function(){
	//图片异常处理
	  var imgTags = document.getElementsByTagName("img");
	  for(var i = 0; i < imgTags.length; i++){
		  imgTags.item(i).onerror = function(){
	            this.src = "/csg_pic/zenithsun/news/mobile/images/Default-bg270x180.png";
	            this.onerror = null;
	      };
	  }
});